<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
    xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
    xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"
    xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om"
    xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"
    xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"
    xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"
    xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa"
    xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl"
    xmlns:tns="${currentFileURL}" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs" version="2.0">
    <!-- updated on 29nd May -->
    <xsl:param name="input" select="document('../Fake_StockTradingGPA.xml')"/>
    <xsl:param name="eventData" select="document('../Fake_MASC_EventData.xml')"/>
    <xsl:param name="measurementData" select="document('../Fake_MASC_MeasurementData.xml')"/>

    <xsl:param name="newline" select="'&#10;'"/>

    <xsl:template match="xmi:XMI">
        <xmi:XMI xmi:version="2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
            xmlns:MASC_Profile_1="http:///schemas/MASC_Profile/_N9zfQCQnEd6djpX8HIz-uw/12"
            xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
            xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
            xsi:schemaLocation="http://schemas.xmlsoap.org/ws/2004/09/policy 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-gp.xsd
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-se.xsd
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ex.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-sc.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-om.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ap.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-up.xsd 
            http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp 
            C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-mp.xsd">
            <xsl:value-of select="$newline"/>
            <xsl:apply-templates select="uml:Model"/>
            <xsl:value-of select="$newline"/>
            <xsl:apply-templates select="MASC_Profile:*"/>
            <xsl:value-of select="$newline"/>
        </xmi:XMI>
    </xsl:template>
    <!-- template for UML Model in XMI -->
    <xsl:template match="uml:Model">
        <xsl:copy-of select="/xmi:XMI/uml:Model"/>
    </xsl:template>

    <!-- template for MASC Profile stereotype in XMI -->
    <xsl:template match="MASC_Profile:*">
        <xsl:for-each select=".">
            <!-- get the MASCID from input XMI's stereotype -->
            <xsl:variable name="MASCID"
                select="@AP-MASCID|@GP-MASCID|@MD-MASCID|@MP-MASCID|@UP-MASCID"/>
            <!-- find the corresponding latest runtime data value in MeasurementData file (by matching MASCID with MSD-MASCID) -->
            <xsl:variable name="valueInMeasurement"
                select="$measurementData/MASCMeasurementData/ProcessInstanceGroup[./ProcessInstance[1]/Data[1]/@MSD-MASCID=$MASCID]/ProcessInstance[1]/Data[1]/Value/text()"/>
            <!-- copy MASC_Profile elements -->
            <xsl:element name="{name()}">
                <!-- copy attributes -->
                <xsl:for-each select="@*">
                    <xsl:attribute name="{name()}">
                        <xsl:choose>
                            <!-- insert value for attribute "LatestRuntimeData" -->
                            <xsl:when
                                test="(name()='AP-LatestRuntimeData') 
                            or (name()='GP-LatestRuntimeData')
                            or (name()='MD-LatestRuntimeData')
                            or (name()='MP-LatestRuntimeData')
                            or (name()='UP-LatestRuntimeData')">
                                <xsl:value-of select="$valueInMeasurement"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="."/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:for-each>
            </xsl:element>
            <xsl:value-of select="$newline"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
